package com.hntech.base.io.bio.v2;

import com.hntech.util.IOUtil;

import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

/**
 * @Autho changqi.wu
 * @Date 路在脚下，使劲踩！
 */
public class IOHandler implements Runnable {

    private Socket socket;
    private static int count = 0;

    public IOHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {

        try {
            InputStream inputstream = socket.getInputStream();
            System.out.println("request : " + IOUtil.readSocketInputStream(inputstream));
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(IOUtil.stringToByte("response " + count++));
            outputStream.flush();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
            bw.write("  response " + count++);
            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
